package com.yunc.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.CommonController;
import com.yunc.common.aspect.BaseLog;
import com.yunc.common.schedule.SchedulerUtils;
import com.yunc.common.utils.BaseConstant;
import com.yunc.common.utils.R;
import com.yunc.modules.sys.entity.SysJob;
import com.yunc.modules.sys.service.ISysJobService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 定时任务
 *
 * @author congcong tao
 * @date 2018/11/12 10:21
 */
@RestController
@RequestMapping("sysJob")
public class SysJobController extends CommonController {

    private static final Logger logger = LogManager.getLogger(SysJobController.class);

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private ISysJobService sysJobService;
    
    @GetMapping("/page")
    public R page(Page<SysJob> page, SysJob sysJob){
        sysJobService.page(page, new QueryWrapper<>(sysJob));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    /**
     * 定时任务信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        SysJob job = sysJobService.getById(id);
        return R.ok().put("sysJob", job);
    }

    /**
     * 保存
     */
    @BaseLog("定时任务")
    @PostMapping("/save")
    @RequiresPermissions("sys:job:save")
    public R save(@RequestBody SysJob job){
        boolean flag = sysJobService.save(job);
        if (flag) {
            SchedulerUtils.createScheduleJob(job, scheduler);
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @BaseLog("定时任务")
    @PutMapping(value = "/update")
    @RequiresPermissions("sys:job:update")
    public R update(@RequestBody SysJob job){
        boolean flag = sysJobService.updateById(job);
        if (flag) {
            SchedulerUtils.updateScheduleJob(scheduler, job);
            return R.ok();
        }
        return R.error();
    }

    @BaseLog("定时任务")
    @PutMapping(value = "/resumeJob/{id}")
    @RequiresPermissions("sys:job:resumeJob")
    public R resumeJob(@PathVariable Long id){
        SysJob job = sysJobService.getById(id);
        job.setStatus(BaseConstant.SchedulerStatus.NORMAL.getValue());
        boolean flag = sysJobService.updateById(job);
        if (flag) {
            SchedulerUtils.resumeJob(scheduler, job);
            return R.ok();
        }
        return R.error();
    }

    @BaseLog("定时任务")
    @PutMapping(value = "/pauseJob/{id}")
    @RequiresPermissions("sys:job:pauseJob")
    public R pauseJob(@PathVariable Long id){
        SysJob job = sysJobService.getById(id);
        job.setStatus(BaseConstant.SchedulerStatus.PAUSE.getValue());
        boolean flag = sysJobService.updateById(job);
        if (flag) {
            SchedulerUtils.pauseJob(scheduler, job.getJobName(), job.getJobGroup());
            return R.ok();
        }
        return R.error();
    }

    @PutMapping(value = "/run/{id}")
    public R run(@PathVariable Long id){
        SysJob jobRecord = sysJobService.getById(id);
        SchedulerUtils.runOne(scheduler, jobRecord);
        return R.ok();
    }
}
